admin: Add new run-triggers command
authorColin Walters <walters@verbum.org>
Sat, 22 Dec 2012 18:43:44 +0000 (13:43 -0500)
committerColin Walters <walters@verbum.org>
Sat, 22 Dec 2012 19:52:33 +0000 (14:52 -0500)
In some cases we want the ability to run triggers independently of
checking out a tree.  For example, due to kernel limitations which
impact the gnome-ostree build system, we may need to run triggers on
first boot via systemd.

Secondarily, if the user installs a system extension which adds a new
shared library to /usr/lib for example, the system will need to run
the triggers again.

Also, I think I want to take triggers out of the core and put them in
ostree admin anyways.

Makefile-ostree.am
src/ostree/ot-admin-builtin-run-triggers.c [new file with mode: 0644]
src/ostree/ot-admin-builtins.h
src/ostree/ot-admin-functions.c
src/ostree/ot-admin-functions.h
src/ostree/ot-builtin-admin.c

index 0066765c932a7781bf99d9e01925671645765683..bc5bba568d10d7bda023b0e5c0662cd32b1ab5a5 100644 (file)
@@ -55,6 +55,7 @@ ostree_SOURCES += \
        src/ostree/ot-admin-builtin-pull-deploy.c \
        src/ostree/ot-admin-builtin-os-init.c \
        src/ostree/ot-admin-builtin-install.c \
+       src/ostree/ot-admin-builtin-run-triggers.c \
        src/ostree/ot-admin-builtin-upgrade.c \
        src/ostree/ot-admin-builtin-update-kernel.c \
        src/ostree/ot-admin-builtins.h \
diff --git a/src/ostree/ot-admin-builtin-run-triggers.c b/src/ostree/ot-admin-builtin-run-triggers.c
new file mode 100644 (file)
index 0000000..7c146cd
--- /dev/null
@@ -0,0 +1,75 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters@verbum.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Author: Colin Walters <walters@verbum.org>
+ */
+
+#include "config.h"
+
+#include "ot-admin-builtins.h"
+#include "ot-admin-functions.h"
+#include "ostree.h"
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <glib/gi18n.h>
+
+static GOptionEntry options[] = {
+  { NULL }
+};
+
+gboolean
+ot_admin_builtin_run_triggers (int argc, char **argv, GFile *ostree_dir, GError **error)
+{
+  GOptionContext *context;
+  gboolean ret = FALSE;
+  ot_lobj GFile *rootdir = NULL;
+  __attribute__((unused)) GCancellable *cancellable = NULL;
+
+  context = g_option_context_new ("[ROOT] - Run triggers (regenerate caches, etc.)");
+  g_option_context_add_main_entries (context, options, NULL);
+
+  if (!g_option_context_parse (context, &argc, &argv, error))
+    goto out;
+
+  if (argc >= 2)
+    {
+      rootdir = g_file_new_for_path (argv[1]);
+    }
+  else
+    {
+      if (!ot_admin_get_sysroot_from_proc_cmdline (&rootdir, cancellable, error))
+        goto out;
+      if (rootdir == NULL)
+        {
+          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                       "No ostree= kernel argument found");
+          goto out;
+        }
+    }
+  
+  if (!ostree_run_triggers_in_root (rootdir, cancellable, error))
+    goto out;
+
+  ret = TRUE;
+ out:
+  if (context)
+    g_option_context_free (context);
+  return ret;
+}
index 2d535f0fa1af915c24b89c34998cc86e33539912..d6decd438035b65ca842d0191f7f96447bbcc3e5 100644 (file)
@@ -34,6 +34,7 @@ gboolean ot_admin_builtin_deploy (int argc, char **argv, GFile *ostree_dir, GErr
 gboolean ot_admin_builtin_prune (int argc, char **argv, GFile *ostree_dir, GError **error);
 gboolean ot_admin_builtin_pull_deploy (int argc, char **argv, GFile *ostree_dir, GError **error);
 gboolean ot_admin_builtin_diff (int argc, char **argv, GFile *ostree_dir, GError **error);
+gboolean ot_admin_builtin_run_triggers (int argc, char **argv, GFile *ostree_dir, GError **error);
 gboolean ot_admin_builtin_update_kernel (int argc, char **argv, GFile *ostree_dir, GError **error);
 gboolean ot_admin_builtin_upgrade (int argc, char **argv, GFile *ostree_dir, GError **error);
 
index 7623062ac918ef709bc1bde3acdf08eb455a75e9..e6903bb2357769daec6b2309573d41404f13f998 100644 (file)
@@ -180,3 +180,41 @@ ot_admin_get_previous_deployment (GFile           *ostree_dir,
   return query_symlink_target_allow_noent (previous_path, out_deployment,
                                            cancellable, error);
 }
+
+gboolean
+ot_admin_get_sysroot_from_proc_cmdline (GFile        **out_deploy_target,
+                                        GCancellable  *cancellable,
+                                        GError       **error)
+{
+  gboolean ret = FALSE;
+  gs_unref_object GFile *proc_cmdline = g_file_new_for_path ("/proc/cmdline");
+  gs_unref_object GFile *ret_deploy_target = NULL;
+  gs_free char *contents = NULL;
+  gsize contents_len;
+  char **cmdline_argv = NULL;
+  char **iter;
+
+  if (!g_file_load_contents (proc_cmdline, cancellable, &contents, &contents_len, NULL,
+                             error))
+    goto out;
+
+  cmdline_argv = g_strsplit (contents, " ", -1);
+  
+  for (iter = cmdline_argv; *iter; iter++)
+    {
+      const char *arg = *iter;
+      if (strncmp (arg, "ostree=", 7) == 0)
+        {
+          gs_free char *subpath = g_strdup (arg + 7);
+          gs_unref_object GFile *deploydir = g_file_new_for_path ("/sysroot/ostree/deploy");
+          ret_deploy_target = g_file_resolve_relative_path (deploydir, subpath);
+          break;
+        }
+    }
+
+  ret = TRUE;
+  ot_transfer_out_value (out_deploy_target, &ret_deploy_target);
+ out:
+  g_strfreev (cmdline_argv);
+  return ret;
+}
index 3c21ad1628bb74cd851fbbab2f86ff46115aaa85..766a3e3855c18523d9d46f2f5799182f1140929c 100644 (file)
@@ -42,6 +42,10 @@ gboolean ot_admin_get_previous_deployment (GFile           *ostree_dir,
                                            GCancellable    *cancellable,
                                            GError         **error);
 
+gboolean ot_admin_get_sysroot_from_proc_cmdline (GFile        **out_deploy_target,
+                                                 GCancellable  *cancellable,
+                                                 GError       **error);
+
 G_END_DECLS
 
 #endif
index e80ac03a7070ffd58460ba443eeb6341e2d89c33..831e80cb10f9edcf5e8eb2dd2aaf2829164fa176 100644 (file)
@@ -52,6 +52,7 @@ static OstreeAdminCommand admin_subcommands[] = {
   { "prune", ot_admin_builtin_prune },
   { "update-kernel", ot_admin_builtin_update_kernel },
   { "config-diff", ot_admin_builtin_diff },
+  { "run-triggers", ot_admin_builtin_run_triggers },
   { NULL, NULL }
 };